package com.tensquare.qa.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.tensquare.qa.pojo.Problem;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 问题的数据访问接口
 * @author Administrator
 *
 */
public interface ProblemDao extends JpaRepository<Problem,String>,JpaSpecificationExecutor<Problem>{
    /**
     * 根据标签id查询最新问题列表，基于PLSQL实现，不推荐使用
     * @param labelid
     * @param pageable
     * @return
     */
    @Query("select p from Problem p where id in (select problemid from Pl where labelid=?1) order by replytime desc")
	public Page<Problem> findNewListByLabelId1(String labelid, Pageable pageable);
    /**
     * 根据标签ID查询热门问题列表
     * @param labelId
     * @param pageable
     * @return
     */
    @Query("select p from Problem p where id in( select problemid from Pl where labelid=?1 ) order by reply desc")
    public Page<Problem> findHotListByLabelId1(String labelId, Pageable pageable);
    /**
     * 根据标签ID查询等待回答列表
     * @param labelId
     * @param pageable
     * @return
     */
    @Query("select p from Problem p where id in( select problemid from Pl where labelid=?1 ) and reply=0 order by createtime desc")
    public Page<Problem> findWaitListByLabelId1(String labelId, Pageable pageable);

    //以下是基于sql语句编写的，推荐使用

    //根据标签id查询最新问题列表
    @Query(value = "select * from tb_problem,tb_pl where id=problemid and labelid=? order by replytime desc",nativeQuery = true)
    public Page<Problem> findNewListByLabelId(String labelid, Pageable pageable);
    //根据标签ID查询热门问题列表
    @Query(value = "select * from tb_problem,tb_pl where id=problemid and labelid=? order by reply desc",nativeQuery = true)
    public Page<Problem> findHotListByLabelId(String labelid, Pageable pageable);
    //根据标签ID查询等待回答的问题列表
    @Query(value="select * from tb_problem,tb_pl where id=problemid and labelid=? and reply=0",nativeQuery = true)
    public Page<Problem> findWaitListByLabelId(String labelId, Pageable pageable);

    @Query(value = "select * from tb_problem t order by visits desc",nativeQuery = true)
    public Page<Problem> findCommendList(Pageable pageable);

    //查询前几个的问题列表
    @Query(value = "select * from tb_problem order by visits desc limit ?",nativeQuery = true)
    List<Problem> findTop(int num);

    //根据问题id查询对应的标签id
    @Query(value = "select labelid from tb_pl where problemid=?",nativeQuery = true)
    int getLabelId(String id);

    //根据用户id查询问题列表并按照时间排序
    @Query(value = "select * from tb_problem where userid=? order by createtime desc",nativeQuery = true)
    public Page<Problem> getProblemList(String id,Pageable pageable);
}
